package com.xinruke.hostels.reservation.service;

import com.xinruke.hostels.common.cache.ReservationUserCache;
import com.xinruke.hostels.common.exception.ArgumentException;
import com.xinruke.hostels.common.po.YwYudingDingdan;
import com.xinruke.hostels.common.po.constants.YwYudingDingdanConstants;
import com.xinruke.hostels.common.po.constants.YwYudingDingdanZixiangConstants;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.common.vo.query.RowsDataVO;
import com.xinruke.hostels.dailymanage.interfaces.api.DailyManageApi;
import com.xinruke.hostels.dailymanage.interfaces.dto.*;
import com.xinruke.hostels.reservation.interfaces.dto.RoomOrderCommentSubmitQueryDto;
import com.xinruke.hostels.reservation.mapper.YwYudingDingdanMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author GuoZheng
 * @ClassName CommentService
 * @date：2019/6/23
 * @version: V1.0.0
 * @description：
 */
@Service
public class CommentService {
    @Resource
    private DailyManageApi dailyManageApi;

    @Resource
    private ReservationUserCache reservationUserCache;

    @Resource
    private RoomOrderService roomOrderService;

    @Resource
    private YwYudingDingdanMapper ywYudingDingdanMapper;

    @Transactional
    public ResultVO<String> submit(CommentSubmitInfoDto params) {
        RoomOrderCommentSubmitQueryDto commentQueryDto = new RoomOrderCommentSubmitQueryDto();
        commentQueryDto.setDingdanhao(params.getDuiyingdingdanhao());

        //校验是否可以提交评论
        List<YwYudingDingdan> mhDingdans = ywYudingDingdanMapper.selectByExample(Example.builder(YwYudingDingdan.class)
                .where(Sqls.custom()
                        .andEqualTo("shanchubiaoshi", YwYudingDingdanZixiangConstants.SHANCHUBIAOSHI_BUSHANCHU)
                        .andEqualTo("dingdanhao", commentQueryDto.getDingdanhao())
                        .andEqualTo("huiyuanId", reservationUserCache.getCurrentUserId())

                ).build());
        if (CollectionUtils.isEmpty(mhDingdans)) {
            throw new ArgumentException("订单不存在");
        }
        YwYudingDingdan mhDingdan = mhDingdans.get(0);
        if (!mhDingdan.getZhuangtai().equals(YwYudingDingdanConstants.ZHUANGTAI_YIWANCHENG)) {
            throw new ArgumentException("该订单未完成");
        }
        if (!mhDingdan.getPingjiazhuangtai().equals(YwYudingDingdanConstants.PINGJIAZHUANGTAI_WEIPINGJIA)) {
            throw new ArgumentException("该订单已评价过");
        }

        //更改对订单状态
        ResultVO orderResultVo = roomOrderService.submitComment(commentQueryDto);
        if (orderResultVo.getCode() != ResultVO.SUCCESS) {
            throw new ArgumentException("更新订单对应的评价状态失败");
        }

        //提交评论
        params.setHuiyuanId(reservationUserCache.getCurrentUserId());
        ResultVO<String> submit = dailyManageApi.submit(params);

        //如果提交评论失败，抛错进行回滚
        if (submit.getCode() == ResultVO.FAIL) {
            throw new ArgumentException("提交评论失败");
        }

        return submit;
    }

    public ResultVO<CommentDetailQueryResultDTO> detail(CommentDetailQueryDTO params) {
        return dailyManageApi.detail(params);
    }

    public ResultVO<RowsDataVO<CommentInfoQueryResultDTO>> list(CommentInfoQueryDto commentInfoQueryDto) {
        return dailyManageApi.list(commentInfoQueryDto);
    }
}
